import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# Load your Excel file
file_path = r'C:\Users\SBD Lab\Desktop\BE_2024_monthly.xlsx'
df = pd.read_excel(file_path)

# Clean and prepare the data
df.columns = ['Datetime', 'Carbon_intensity']
df['Datetime'] = pd.to_datetime(df['Datetime'], dayfirst=True, errors='coerce')
df.dropna(subset=['Datetime', 'Carbon_intensity'], inplace=True)

# Extract month number
df['Month'] = df['Datetime'].dt.month

# Define a function to map month to season
def month_to_season(month):
    if month in [12, 1, 2]:
        return 'Winter'
    elif month in [3, 4, 5]:
        return 'Spring'
    elif month in [6, 7, 8]:
        return 'Summer'
    else:
        return 'Fall'

# Apply the function
df['Season'] = df['Month'].apply(month_to_season)

# Group by Season and calculate average carbon intensity
season_order = ['Winter', 'Spring', 'Summer', 'Fall']
season_avg = df.groupby('Season')['Carbon_intensity'].mean().reindex(season_order)

# Prepare heatmap data (1 row, 4 columns)
heatmap_data = [season_avg.values]

# Plot
plt.figure(figsize=(8, 2))
ax = sns.heatmap(
    heatmap_data,
    cmap='RdYlGn_r',
    cbar=True,
    vmin=0,
    vmax=300,
    cbar_kws={
        'orientation': 'horizontal',
        'label': 'Carbon Intensity [gCO₂/kWh]',
        'pad': 0.2  # increased padding between heatmap and colorbar
    }
)

# Set x-axis with seasons
plt.xticks(ticks=[i + 0.5 for i in range(4)], labels=season_order, fontsize=14)
plt.yticks([])  # hide y-axis since it's a single row

# Title and label with larger font size
plt.title('Seasonal Carbon Intensity (Belgium 2024)', fontsize=18, fontweight='bold')
plt.xlabel('Season (2024)', fontsize=13, labelpad=12)

plt.subplots_adjust(bottom=0.3)  # space below x-axis and colorbar
plt.tight_layout()
plt.show()
